/*
 * redmicro all Copyright (c)
 */

/**
 * Created by seeker910 on 2015/5/27.
 */

Rsd.define('Rsd.Faith.dialogs.BizZiJinShiYongFenTanDialog', {
    extend: 'Rsd.view.MajorSubDialog',
    xtype:'fentan-dialog',
    width:1200,
    height:800,
    major:{
        fk:'ShenQingDanHao',
        legend:'',
        header:{content:'&nbsp资金使用信息',position:'top',height:40,align:'left',width:'100%'},
        height:250,
        readOnly:true,
        fields:[
            {
                "name": "ShenQingDanHao",
                "label": {
                    "text": "申请单号",
                    "width": 120
                },
                width:350,
                "xtype": "text"
            },
            {
                "name": "ShenQingJinE",
                "label": {
                    "text": "申请金额",
                    "width": 120
                },
                width:350,
                "xtype": "number",
                "precision": 2
            },
            {
                "name": "ShenQingJinEDaXie",
                "label": {
                    "text": "申请金额大写",
                    "width": 120
                },width:350,
                "xtype": "text"
            },
            {
                "name": "ShenQingRiQi",
                "label": {
                    "text": "申请日期",
                    "width": 120
                },
                width:350,
                "xtype": "date",
                "formatString": "yyyy-MM-dd"
            },
            {
                "name": "ShenQingDanWei",
                "label": {
                    "text": "申请单位",
                    "width": 120
                },
                width:350,
                "xtype": "text"
            },
            {
                "name": "ShenQingDanWeiFaRenDaiBiao",
                "label": {
                    "text": "申请单位法人代表",
                    "width": 120
                },
                width:350,
                "xtype": "text"
            },
            {
                "name": "XiaoQuBianHao",
                "label": {
                    "text": "小区编号",
                    "width": 120
                },
                width:350,
                "xtype": "text"
            },
            {
                "name": "LouDongHao",
                "label": {
                    "text": "楼栋号",
                    "width": 120
                },width:350,
                "xtype": "text"
            },
            {
                "name": "ZhuangTai",
                "label": {
                    "text": "状态",
                    "width": 120
                },
                width:350,
                "xtype": "text"
            },
            {
                "name": "BoFuFangShi",
                "label": {
                    "text": "拨付方式",
                    "width": 120
                },
                width:350,
                "xtype": "text"
            },
            {
                "name": "ShenQingShiXiang",
                "label": {
                    "text": "申请事项",
                    "width": 120
                },
                width:700,
                "xtype": "text"
            },
            {
                "name": "Remark",
                "label": {
                    "text": "备注",
                    "width": 120
                },width:350,
                "xtype": "text"
            },
            {
                "name": "WeiXiuFanWei",
                "label": {
                    "text": "维修范围",
                    "width": 120
                },
                width:700,
                "xtype": "text"
            }

        ]
    },
    sub:{
        fk:'ShenQingDanHao',
        modelType:'Rsd.Faith.Models.UsingZiJinShiYongFenTanBiaoModel',
        dataSource:'listStore',
        toolBar:{
            xtype:'grid-tool-bar-simple',
            newButtons:[
                {xtype:'label',text:'★ 下方列表为本次资金使用各户分摊明细。👇',width:250}

                ]},
        columns: [
            {
                "text": "选择",
                name:'IsChecked',
                editable:true,
                xtype:'checkbox'
            },
            {
                "text": "序号",
                "xtype": "index",
                "width": 40,
                "index": 0
            },
            {
                //"xtype":"template",
                "name": "BianHao",
                "text": "房屋编号",
                "sortable": true,
                "index": 0,
                "width": 150,
                "nobr": true
            },
            {
                "name": "LouDongHao",
                "text": "楼栋号",
                "sortable": true,
                "index": 0,
                "align":"center",
                "width": 50,
                "nobr": true
            },
            {
                "name": "DanYuanHao",
                "text": "单元号",
                "sortable": true,
                "index": 0,
                "align":"center",
                "xtype": "number",
                "width": 50
            },
            {
                "name": "ShiHao",
                "text": "室号",
                "sortable": true,
                "index": 0,
                "align":"center",
                "width": 50,
                "nobr": true
            },
            {
                "name": "YeZhuXingMing",
                "text": "业主姓名",
                "sortable": false,
                "index": 0,
                "width": 70,
                "nobr": true
            },
            {
                "name": "FangWuLeiXing",
                "text": "房屋类型",
                "sortable": false,
                "index": 0,
                "width": 70,
                "align":"center",
                "nobr": true
            },
            {
                "name": "FangWuXingZhi",
                "text": "房屋性质",
                "sortable": false,
                "index": 0,
                "width": 70,
                "nobr": true
            },
            {
                "name": "JianZhuMianJi",
                "text": "建筑面积",
                "sortable": false,
                "index": 0,
                "xtype": "number",
                "width": 80,
                "precision": 0
            },
            {
                "name": "Remark",
                "text": "备注",
                "sortable": false,
                "index": 200,
                "width": 50,
                "nobr": true
            }
        ]

    },
    footer:{
       height:55,style:{fontSize:'120%',left:'50px'},
        items:[
            {xtype:'checkbox',handler:'chk_all_click',label:{text:'全选',position: 'right'},width:80,style:{top:10}},
            {
                xtype:'button',height:40,width:500,text:'Ⓢ确 定 分 摊',handler:'btn_ok',
                label:{content:'分摊数据在入账前，可重新分摊',position:'right',width:350,style:{lineHeight:40}}
            }]
    },
    /*
    * */
    constructor: function (config) {
        config = config || {};
        Rsd.apply(this, config);
        this.title = '资金使用分摊[申请单号：' + this.data['ShenQingDanHao'] +']';

    },

    /*
    * */
    load:function load() {
        var me = this;
        var panel = Rsd.widget('container',{layout:'hbox',items:[{width:300}],floating:true,width:'100%',height:50,style:{backgroundColor:'#a6caca66',top:255}});

        me.add(panel);
         if(Rsd.isEmpty(this.data['ShenQingDanHao']))
         {
             return;
         }
         Rsd.app.services['using.getshenqininfo'].requestJson({ shenQingDanHao:this.data['ShenQingDanHao']},function (shengqing) {

             if(shengqing.success)
             {
                 me.shengQin = shengqing.data;
                 me.loadMajor(shengqing.data);

                 if(shengqing.data.LouDongHao )
                 {
                     // 楼栋号 或 有单元号 分摊到户
                     Rsd.app.requestService('info.GetLouDongList',
                         {
                             XiaoQuBianHao:shengqing.data.XiaoQuBianHao,
                             LouDongHao:shengqing.data.LouDongHao,
                             DanYuanHao:shengqing.data.DanYuanHao
                         },
                         function (loudong) {
                             //console.log(loudong);
                             if(loudong.success)
                             {
                                 me.danyuanlist={};
                                 for(var i in loudong.data) {
                                     var _danyuanhao = loudong.data[i]['DanYuanHao'];
                                     var _value = 'DY_' + _danyuanhao;

                                     if (Rsd.isEmpty(me.danyuanlist[_value])) {

                                         panel.add({
                                             xtype: 'checkbox',
                                             value: _value,
                                             label: loudong.data[i]['DanYuanHao'] + '单元',
                                             handler: 'chk_click',
                                             width: 90,
                                             style: {top: 10}
                                         });

                                         me.danyuanlist[_value] = {danyuanhao: _danyuanhao, checked: false};
                                     }
                                 }
                             }

                         }
                     );
                 }else
                 {
                     //没有楼栋号 和 单元号  分摊到楼栋
                     Rsd.app.requestService('info.GetLouDongList',
                         {
                             XiaoQuBianHao:shengqing.data.XiaoQuBianHao
                         },
                         function (loudong) {
                             //console.log(loudong);
                             if(loudong.success)
                             {
                                 me.loudonglist={};
                                 for(var i in loudong.data) {
                                     var _loudonghao = loudong.data[i]['LouDongHao'];
                                     var _value = 'LD_' + _loudonghao;
                                     if (Rsd.isEmpty(me.loudonglist[_value])) {

                                         panel.add({
                                             xtype: 'checkbox',
                                             value: _value,
                                             label: _loudonghao + '栋',
                                             handler: 'chk_click',
                                             width: 90,
                                             style: {top: 10}
                                         });

                                         me.loudonglist[_value] = {loudonghao: _loudonghao, checked: false};
                                     }

                                 }
                             }

                         }
                     );
                 }

             }else
             {
                 Rsd.alert(shengqing.msg);
             }

         });

    },
    /*
    * */
    chk_all_click:function(sender,e)
    {
        var me = this;

        for (var i in me.fanwuList)
        {
            me.fanwuList[i]['IsChecked'] = sender.isChecked();
        }

        me.loadSub(me.fanwuList);
    },
    /*
    * */
    chk_click:function chk_click(sender,e) {
        var me = this;
        var loudong =[];
        var danyuan = [];
        var _key = sender.getValue();
        //console.log(_key);
        if(me.shengQin.LouDongHao)
        {

            if(!Rsd.isEmpty(me.danyuanlist[_key]))
            {
                me.danyuanlist[_key].checked = sender.isChecked();
            }
            for(var i in me.danyuanlist)
            {
                if( me.danyuanlist[i].checked)
                {
                    danyuan.push(me.danyuanlist[i].danyuanhao);
                }
            }
        }
        else
        {

            if(!Rsd.isEmpty(me.loudonglist[_key]))
            {
                me.loudonglist[_key].checked = sender.isChecked();
            }
            for(var i in me.loudonglist)
            {
                if( me.loudonglist[i].checked)
                {
                    loudong.push(me.loudonglist[i].loudonghao);
                }
            }
        }

        //console.log(me.loudonglist);

        Rsd.app.requestService('info.FerFangWuList',  {
                XiaoQuBianHao:me.shengQin.XiaoQuBianHao,
                LouDongHao:loudong.join(','),
                DanYuanHao:danyuan.join(',')
            },function (data) {
            me.fanwuList = data.data;
            me.loadSub(data.data);
        });
    },
    /*
    * */
    btn_ok:function () {

        var me = this;
        var shengQingDanHao = this.data['ShenQingDanHao'];
        var list = [];
        for (var i in me.fanwuList)
        {
           if( me.fanwuList[i]['IsChecked'] ){
               list.push(me.fanwuList[i]['BianHao']);
           }
        }
        if(list.length == 0)
        {
            Rsd.alert('未选择房屋,无法分摊');
            return ;
        }
        //Using/FangWuFengTan
        Rsd.app.requestService('Using.FangWuFengTan',{shenQingDanHao:shengQingDanHao,fangWuDaiMaList:list.join(',')},function(fentan){

            if(fentan.success)
            {
                //Using/FengTanQueRen
                Rsd.app.requestService('Using.FengTanQueRen',{shenQingDanHao:shengQingDanHao},function(queren){

                    if(queren.success)
                    {
                        Rsd.popup('分摊数据已提交');
                        me.close();
                    }else
                    {
                        Rsd.alert(queren.msg);
                    }
                });
            }else
            {
                Rsd.alert(fentan.msg);
            }
        });


    }

 });